/**
 * UserPreferencesPanel.java 18 sept. 2006
 *
 * Copyright (c) 2006 Emmanuel PUYBARET / eTeks <info@eteks.com>. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.windu2b.jcaddie.swing;

import java.awt.*;
import java.util.*;

import javax.swing.*;

import org.windu2b.jcaddie.model.*;

/**
 * @author Emmanuel Puybaret, windu.2b
 * 
 */
public class UserPreferencesPanel extends JPanel
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 2344678729897036697L;


	private ResourceBundle	  resource;


	private JLabel	          unitLabel;


	private JRadioButton	  litreRadioButton;


	private JRadioButton	  gallonRadioButton;




	public UserPreferencesPanel()
	{
		super( new GridBagLayout() );
		resource = ResourceBundle.getBundle( UserPreferencesPanel.class
		        .getName() );
		createComponents();
		setMnemonics();
		layoutComponents();
	}




	private void createComponents()
	{
		unitLabel = new JLabel( resource.getString( "unitLabel.text" ) );
		litreRadioButton = new JRadioButton( resource
		        .getString( "litreRadioButton.text" ), true );
		gallonRadioButton = new JRadioButton( resource
		        .getString( "gallonRadioButton.text" ) );

		ButtonGroup unitButtonGroup = new ButtonGroup();
		unitButtonGroup.add( litreRadioButton );
		unitButtonGroup.add( gallonRadioButton );
	}




	private void setMnemonics()
	{
		if ( !System.getProperty( "os.name" ).startsWith( "Mac OS X" ) )
		{
			litreRadioButton.setMnemonic( resource.getString(
			        "litreRadioButton.mnemonic" ).charAt( 0 ) );
			gallonRadioButton.setMnemonic( resource.getString(
			        "gallonRadioButton.mnemonic" ).charAt( 0 ) );
		}
	}




	private void layoutComponents()
	{
		Insets labelInsets = new Insets( 0, 0, 5, 5 );
		add( unitLabel, new GridBagConstraints( 0, 0, 1, 1, 0, 0,
		        GridBagConstraints.WEST, GridBagConstraints.NONE, labelInsets,
		        0, 0 ) );
		add( litreRadioButton, new GridBagConstraints( 1, 0, 1, 1, 0, 0,
		        GridBagConstraints.WEST, GridBagConstraints.NONE, labelInsets,
		        0, 0 ) );
		Insets rightComponentsInsets = new Insets( 0, 0, 5, 0 );
		add( gallonRadioButton, new GridBagConstraints( 2, 0, 1, 1, 0, 0,
		        GridBagConstraints.WEST, GridBagConstraints.NONE,
		        rightComponentsInsets, 0, 0 ) );
	}




	public void setPreferences( UserPreferences preferences )
	{
		if ( preferences.getUnit() == UserPreferences.Unit.LITRE )
			litreRadioButton.setSelected( true );
		else litreRadioButton.setSelected( false );
	}




	public boolean showDialog( JComponent parent )
	{
		String dialogTitle = resource.getString( "preferences.title" );

		return JOptionPane.showConfirmDialog( parent, this, dialogTitle,
		        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE ) == JOptionPane.OK_OPTION;
	}




	public UserPreferences.Unit getUnit()
	{
		if ( litreRadioButton.isSelected() )
			return UserPreferences.Unit.LITRE;
		else return UserPreferences.Unit.GALLON;
	}
}